var address = "172.20.41.71:7071";
var newToken = layui.sessionData("userToken").key;
//下面ws/ 后面是用户token
var url = "ws://" + address + "/ws/" + newToken;
//判断浏览器是否支持websocket
if ("WebSocket" in window) {
  //如果支持，创建websocket对象，注意url格式
  websocket = new ReconnectingWebSocket(url);
  websocket.debug = true;
  //websocket.timeoutInterval=2000;重连的最长等待时间
} else {
  alert("浏览器版本不支持websocket！");
}
//定义连接错误的回调方法
websocket.onerror = function () {
  console.log("websocket连接出错！");
};
//定义连接成功的回调方法
websocket.onopen = function () {
  console.log("websocket连接成功！");
};
//定义websocket关闭的回调方法
websocket.onclose = function () {
  console.log("websocket已关闭！");
};

//当窗口关闭时，主动去关闭websocket连接
window.onbeforeunload = function () {
  closeWebSocket();
};

//接收到消息的回调方法
websocket.onmessage = function (event) {
  var arr = JSON.parse(event.data);
  layer.open({
    type: 1,
    offset: "t",
    area: ["20%", "20%"],
    content:
      '<div class="messageBox">' +
      '<div class="messageIcon"><i class="layui-icon layui-icon-dialogue"></i></div>' +
      "<div>" +
      arr["data"] +
      "</div></div>",
    shade: 0, //不显示遮罩
    success: function (layero, index) {
      // $(".messageNotification").html();
    },
    yes: function () {
      layer.closeAll();
    },
  });
};
